﻿package com.assist.utils
{
    public class TimeUtil extends Object
    {
		public static const HOUR_TAG:String = '{!hour}';
		public static const MINUTE_TAG:String = '{!minute}';
		public static const SECOND_TAG:String = '{!second}';

		public static const DEFAULT_SECOND_FORMAT:String = HOUR_TAG + ":" + MINUTE_TAG + ":" + SECOND_TAG;

		public static function formatSecond(second:uint, format:String=null) : String
		{
			if (format == null) {
				format = DEFAULT_SECOND_FORMAT;
			}
			
			var hour:uint = Math.floor(second / 3600);
			var minute:uint = Math.floor((second - hour * 3600) / 60);
			var second:uint = second % 60;

			var pairs:Object = {};
			pairs[HOUR_TAG] = IntUtil.format(hour, 2);
			pairs[MINUTE_TAG] = IntUtil.format(minute, 2);
			pairs[SECOND_TAG] = IntUtil.format(second, 2);
			return StringUtil.replace(format, pairs);			
		}
		
		public static function formatYearAndDay(second:uint, format:String=null):String
		{
			if (format == null) {
				format = DEFAULT_SECOND_FORMAT;
			}
			
			var time:Date = new Date(second * 1000);
			
			var year:uint = time.fullYear;
			var month:uint = time.month + 1;
			var day:uint = time.date;
			var hour:uint = time.hours;
			var mimutes:uint = time.minutes;
			var seconds:uint = time.seconds;
			
			var str:String = String(year) + "/" + String(month) + "/" + String(day) + "-" + String(hour) + ":" + String(mimutes) + ":" + String(seconds);
			return str;
		}
		
		public static function formatSecondByRange(second:uint, pairs:Object) : String
		{
			var keys:Array = ObjectUtil.getKeys(pairs);
			keys.sort(Array.NUMERIC);
			for each (var key:uint in keys) {
				if (second < key) {
					if (pairs[key] is Function) {
						return pairs[key](second);
					}
					else {
						return pairs[key];
					}
				}
			}
			return formatSecond(second);
		}
		
		public static function formatTimestamp(timestamp:uint, format:String) : String
		{
			throw new Error("method not implement");
		}		
    }
}
